package sctu.edu.java.year2016.team07.huangsuixin;

import java.sql.*;

/**
 * Created by Huang Suixin on 2016/6/12.
 */
public class JDBCDemo {
    public static void main(String[] args) {

        String url = "jdbc:mysql://localhost:3306/java?serverTimezone=UTC&useSSL=false";
        String user = "root";
        String password = "";
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            /*Connection conn = DriverManager.getConnection(url,user,password);
            String sql = "insert into student(id,name,age) values('','help','20')";
            Statement stmt = conn.createStatement();
            stmt.executeUpdate(sql);

            conn.close();

            System.out.println("添加成功！");*/
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("驱动加载失败！");
        } //catch (SQLException e) {
            //e.printStackTrace();
           // System.out.println("连接失败");
        //}

        try {
            Connection conn = DriverManager.getConnection(url,user,password);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select id,name,age from student WHERE NAME LIKE '%s%'");

            int id=0,age;
            String name;
            System.out.println("id\t用户名\t年龄");
            System.out.println(rs);
            while(rs.next()){
                id = rs.getInt(1);
                name = rs.getString(2);
                age = rs.getInt(3);
                System.out.println(id+"\t"+name+"\t"+age);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
